/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * alertCreaAlbaran.java
 *
 * Created on 01-may-2009, 10:29:52
 */

package alertas;

import gestion.Albaran;
import gestion.Trabajo;
import gestion.TrabajoProducto;
import productos.Producto;
import laboratorio.*;
import java.util.*;

import javax.persistence.Query;

/**
 *
 * @author Juan Antonio Tirado
 */
public class alertCreaAlbaran extends javax.swing.JFrame {
    //Atributos
    private Albaran albaran;
    private TrabajoProducto trabProd;
    private Producto producto;
    private Trabajos VentanaTrabajosOld;

    private ListIterator ittrabprod;
    private ListIterator itProd;

    private float total;
    private int cantidad;
    private boolean enc;

    private Query q1,q2;

    /** Creates new form alertCreaAlbaran */
    public alertCreaAlbaran() {
        initComponents();
    }
    public alertCreaAlbaran(Trabajos trabajosOld) {
        initComponents();
        VentanaTrabajosOld=trabajosOld;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        trabajoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT t FROM Trabajo t");
        trabajoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : trabajoQuery.getResultList();
        albaranQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT a FROM Albaran a");
        albaranList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : albaranQuery.getResultList();
        trabajoProductoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT t FROM TrabajoProducto t");
        trabajoProductoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : trabajoProductoQuery.getResultList();
        productoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT p FROM Producto p");
        productoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : productoQuery.getResultList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(alertCreaAlbaran.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                crearAlbaran(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                salir(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(171, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(36, 36, 36)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(45, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_salir
        this.setVisible(false);
    }//GEN-LAST:event_salir

    private void crearAlbaran(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_crearAlbaran
        Integer iDalbaran=albaranList.get(albaranList.size()-1).getIDalbaran()+1;
        Trabajo t=VentanaTrabajosOld.getTrabajo();
        q1=lpdPUEntityManager.createQuery("SELECT tp FROM TrabajoProducto tp WHERE tp.trabajoProductoPK.iDtrabajo="+t.getIDtrabajo());
        List trabajosproductos=q1.getResultList();
        Iterator it=trabajosproductos.iterator();
        Date d=new Date();
        String fecha=String.valueOf(d.getDate()).concat("-").concat(String.valueOf(d.getMonth()+1)).concat("-").concat(String.valueOf(d.getYear()+1900));
        total=0;
        cantidad=0;
        while (it.hasNext()){
            TrabajoProducto tp=(TrabajoProducto) it.next();
            cantidad=tp.getCantidad();
            q2=lpdPUEntityManager.createQuery("SELECT p FROM Producto p WHERE p.iDproducto="+tp.getTrabajoProductoPK().getIDproducto());
            producto=(Producto) q2.getSingleResult();
            total=total+(producto.getPrecio()*cantidad);
        }
        albaran=new Albaran(iDalbaran,fecha,t.getIDtrabajo(),total);
        lpdPUEntityManager.getTransaction().begin();
        lpdPUEntityManager.persist(albaran);
        lpdPUEntityManager.getTransaction().commit();
        Albaranes a=new Albaranes();
        a.setVisible(true);
        a.setAlbaranSelec(albaran);
        this.setVisible(false);
    }//GEN-LAST:event_crearAlbaran

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new alertCreaAlbaran().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<gestion.Albaran> albaranList;
    private javax.persistence.Query albaranQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.persistence.EntityManager lpdPUEntityManager;
    private java.util.List<productos.Producto> productoList;
    private javax.persistence.Query productoQuery;
    private java.util.List<gestion.Trabajo> trabajoList;
    private java.util.List<gestion.TrabajoProducto> trabajoProductoList;
    private javax.persistence.Query trabajoProductoQuery;
    private javax.persistence.Query trabajoQuery;
    // End of variables declaration//GEN-END:variables

}
